﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11._12.PolynomsADDSUBS
{
    /*
 *  11.Write a method that adds two polynomials. 
 *  Represent them as arrays of their coefficients as in the example below:
 *
 *  x^2 + 5 = 1x^2 + 0x + 5 -> 5 0 1
 *  
 *  12.Extend the program to support also subtraction and multiplication of polynomials.
 */


    class PolynomsADDSUBS
    {
        static void Main()
        {
            decimal[] polynom1 = new decimal[] { 5, -3, 6, -5, -4 };
            decimal[] polynom2 = new decimal[] { 10, 3, 5 };

            Console.WriteLine("Polynom 1: ".PadLeft(30, ' ') + PolynomsToString(polynom1));
            Console.WriteLine("Polynom 2: ".PadLeft(30, ' ') + PolynomsToString(polynom2));

            //Sum
            decimal[] sumOfPolynoms = SumPolynoms(polynom1, polynom2);
            Console.WriteLine("\n" + "Sum of polynoms: ".PadLeft(30, ' ') + PolynomsToString(sumOfPolynoms));

            //Substraction
            decimal[] substractionOfPolynoms = SubstractPolynoms(polynom1, polynom2);
            Console.WriteLine("\n" + "Substraction: ".PadLeft(30, ' ') + PolynomsToString(substractionOfPolynoms));

            //Multiplication
            decimal[] multiplicationOfPolynoms = MultiplyPolynoms(polynom1, polynom2);
            Console.WriteLine("\n" + "Multiplication: ".PadLeft(30, ' ') + PolynomsToString(multiplicationOfPolynoms));
        }

        private static decimal[] MultiplyPolynoms(decimal[] first, decimal[] second)
        {
            decimal[] result = new decimal[first.Length + second.Length];

            for (int i = 0; i < first.Length; i++)
            {
                for (int y = 0; y < second.Length; y++)
                {
                    result[i + y] = result[i + y] + first[i] * second[y];
                }
            }

            return result;
        }

        //substract polynoms
        private static decimal[] SubstractPolynoms(decimal[] firstPoly, decimal[] secondPoly)
        {
            decimal[] newPolynomial = new decimal[Math.Max(firstPoly.Length, secondPoly.Length)];

            for (int i = 0; i < firstPoly.Length; i++)
            {
                newPolynomial[i] = firstPoly[i];
            }

            for (int i = 0; i < secondPoly.Length; i++)
            {
                newPolynomial[i] = newPolynomial[i] - secondPoly[i];
            }

            return newPolynomial;
        }

        //Sum polynoms
        private static decimal[] SumPolynoms(decimal[] first, decimal[] second)
        {
            decimal[] newPolynom = new decimal[Math.Max(first.Length, second.Length)];

            for (int i = 0; i < first.Length; i++)
            {
                newPolynom[i] = first[i];
            }

            for (int i = 0; i < second.Length; i++)
            {
                newPolynom[i] = newPolynom[i] + second[i];
            }

            return newPolynom;
        }

        //printing polynoms
        private static string PolynomsToString(decimal[] polynom)
        {
            StringBuilder toString = new StringBuilder();

            for (int i = polynom.Length - 1; i >= 0; i--)
            {
                if (polynom[i] != 0)
                {
                    toString.Append(polynom[i] > 0 ? " + " : " - ");
                    toString.Append(Math.Abs(polynom[i]));
                    if (i != 0)
                    {
                        toString.Append(i > 1 ? "x^" + i : "x");
                    }
                }
            }

            if (toString[1] != '-')
            {
                toString.Remove(0, 3);
            }
            else
            {
                toString.Remove(0, 1);
            }

            return toString.ToString();
        }
    }
}